function [data] = FCimport()
% Modified 2014-08-28
%
% Adopted from '131108 adhesion LAS3/FCimportMCv3.m'.
%
% Imports DFL multi-force curve data (see DATA EXPORT below).
%
% OUTPUT:       data, structure with 'approach' and 'retract' cell arrays,
% units [um nA].
%
% DATA EXPORT:
% Can be multiple signals, but will only read the first signal (DFL).
% Export ASCII.
% Values only.
% Include header.
% Delimiters for Values: Enter.
% Data unis: Original.
%
% BEWARE: NT-MDT export sometimes SWITCHES approach and retract curves.
% Program asks user to decide whether to flip data based on this error.


[afile] = uigetfile('.txt','Choose file');

fid = fopen(afile);                         %open file

%Main heading and first data set
b = textscan(fid,'Points = %d Signals = %d Curves = %d','Delimiter', '\n','HeaderLines',8);    %Number of lines before ID of number of curves                             %delimiter set as a new line
npoints = b{1};  %number of points
ncurves = b{3};   %number of curves per point
nsigs = b{2}; %number of signals
Ntotal = b{1}*b{2}*b{3};
skips = 9;     %Number of skips to get to Forward Header (begins with "Forward")

headapp_newpt = 2; %Number of lines to mark a new point
headapp = 13; %Number of lines in the forward header
headret = 13; %Number of lines in the backward header

%All other headings and data sets

fclose('all');

for jjpt = 1:npoints         %for each point...
    for jjcv = 1:ncurves        %for each curve at the point...
        for jjsig = 1:nsigs    %for each signal in the curve...

            jjcrv = jjcv + (jjpt-1)*ncurves;        %Number of the curve out of the total number of curves



            %Retract header
            if jjsig==1 && jjcv==1    %For first header in each point, need to add headapp_newpt to skips
                skips = skips + headapp_newpt;
            end
            fid = fopen(afile);          %Re-open file to restart textscan (required because test of 'c' variable reads first fail at 6 spaces into the data)
            c1 = textscan(fid,...
                '%*s %*s Signal = %s %*s Scale X = %f %*s Bias X = %f %*s %*s %*s %*s %*s Size = %f',...  %Defines exactly what the header looks like and what data is needed
                'Delimiter', '\n',...                     %Delimiter is a new line
                'HeaderLines',skips);                     %Accumulated number of lines to skip
            fclose('all');
            % If the signal is NOT DFL data, the program skips it.
            if ~strcmp(c1{1},'DFL')             
                fid = fopen(afile);          
                c2 = textscan(fid,...           % Import retract header
                    '%*s %*s %*s %*s Scale X = %f %*s Bias X = %f %*s %*s %*s %*s %*s Size = %f',...
                    'Delimiter', '\n',...
                    'HeaderLines',skips+headapp+c1{4});  %Skip number of approach data points
                fclose('all');
                skips = skips + c1{4} + c2{3} + headapp + headret;    %skip all data points in the signal, plus two headers
                continue                    % Skips to the next signal
            end
            z = [0:c1{4}-1].*c1{2};      %Populate z-values (indentation)
            z = z(:);

            if z(1)>z(end)    %Check to see if data is incorrectly x-reflected. Test assumes that 0 is the first point in the FC.
                z = z(end:-1:1);  %Reverse data points
            end
            z = z - z(1);     % Sets the z-data to 0
            
            data.ret{jjcrv}(:,1) = z;



            %Retract data
            fid = fopen(afile);          
            d = textscan(fid,'%f', 'Delimiter','\n',...
                'HeaderLines',skips+headapp,...      %First header is 13 lines
                'CollectOutput', 1);     
            fclose('all');
            app0 = d{1,1};

            if app0(1)<app0(end)    %Check to see if data is incorrectly x-reflected. Test assumes that indentation proceeds from maximum z to 0.
                app0 = app0(end:-1:1);  %Reverse data points
            end

            data.ret{jjcrv}(:,2) = app0;

            %Approach header
            fid = fopen(afile);          
            c2 = textscan(fid,...
                '%*s %*s %*s %*s Scale X = %f %*s Bias X = %f %*s %*s %*s %*s %*s Size = %f',...
                'Delimiter', '\n',...
                'HeaderLines',skips+headapp+c1{4});  %Skip number of approach data points
            fclose('all');
            z = [0:c2{3}-1].*c2{1};
            z = z(:);
            
            if z(1)>z(end)    %Check to see if data is incorrectly x-reflected. Test assumes that 0 is the first point in the FC.
                z = z(end:-1:1);  %Reverse data points
            end
            z = z - z(1);

            data.app{jjcrv}(:,1) = z;

            %Approach data
            fid = fopen(afile); 
            d = textscan(fid,'%f', 'Delimiter','\n',...
                'HeaderLines',skips+headapp+c1{4}+headret,...
                'CollectOutput', 1);     %Retract header is 13 lines
            fclose('all');
            ret0 = d{1,1};
            
            if ret0(1)<ret0(end)    %Check to see if data is incorrectly x-reflected. Test assumes that indentation proceeds from maximum z to 0.
                ret0 = ret0(end:-1:1);  %Reverse data points
            end

            data.app{jjcrv}(:,2) = ret0;

            %Count up total number of lines skipped
            skips = skips + c1{4} + c2{3} + headapp + headret;    %skip all data points, plus two headers
        end
    end
end


